import Taro from '@tarojs/taro'

interface AMapWXOptions {
  key: string;
}

interface LocationOptions {
  location: string;
  radius?: number;
  querytypes?: string;
  querykeywords?: string;
  iconPath?: string;
  iconWidth?: number;
  iconHeight?: number;
  success?: (result: any) => void;
  fail?: (error: any) => void;
}

interface MarkerResult {
  latitude: number;
  longitude: number;
  id: number;
  name: string;
  address: string;
  iconPath: string;
  width: number;
  height: number;
}

class AMapWX {
  private key: string;
  private requestConfig: any;

  constructor(options: AMapWXOptions) {
    this.key = options.key;
    this.requestConfig = {
      key: options.key,
      s: 'rsx',
      platform: 'WXJS',
      appname: options.key,
      sdkversion: '1.2.0',
      logversion: '2.0'
    };
  }

  // 获取周边POI
  getPoiAround(options: LocationOptions): void {
    const requestData = {
      key: this.key,
      location: options.location,
      radius: options.radius || 3000,
      types: options.querytypes || '',
      keywords: options.querykeywords || '',
      extensions: 'all',
      s: this.requestConfig.s,
      platform: this.requestConfig.platform,
      appname: this.requestConfig.appname,
      sdkversion: this.requestConfig.sdkversion,
      logversion: this.requestConfig.logversion
    };

    Taro.request({
      url: 'https://restapi.amap.com/v3/place/around',
      data: requestData,
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: (res: any) => {
        if (res.data && res.data.status === '1' && res.data.pois) {
          const markers: MarkerResult[] = res.data.pois.map((poi: any, index: number) => {
            const location = poi.location.split(',');
            return {
              latitude: parseFloat(location[1]),
              longitude: parseFloat(location[0]),
              id: index,
              name: poi.name,
              address: poi.address || '',
              iconPath: options.iconPath || '',
              width: options.iconWidth || 22,
              height: options.iconHeight || 32
            };
          });
          
          options.success && options.success({
            markers: markers,
            poisData: res.data.pois
          });
        } else {
          options.fail && options.fail({
            errCode: res.data.infocode || '0',
            errMsg: res.data.info || '搜索失败'
          });
        }
      },
      fail: (error: any) => {
        console.error('请求失败:', error)
        if (options.fail) {
          options.fail(error)
        }
      }
    })
  }

  // 获取当前位置
  getWxLocation(options: any, callback?: (location: string) => void): void {
    Taro.getLocation({
      type: 'gcj02',
      success: (res: any) => {
        const location = res.longitude + ',' + res.latitude;
        callback && callback(location);
      },
      fail: (err: any) => {
        options.fail && options.fail(err);
      }
    });
  }
}

export default AMapWX;